home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 476-500 / disk_500 / wiconify / wiconcalls.lzh / wExample1 / wExample1.c < prev    next >
C/C++ Source or Header  |  1991-04-19  |  7KB  |  313 lines

  1. /*
  2.  *  WEXAMPLE1.C     Example program for wIconify.
  3.  *                  This program simply adds an icon to its window, and
  4.  *                  then iconifies it.  It's pretty trivial otherwise.
  5.  */
  6.  
  7. #define INTUITION_PREFERENCES_H             /* don't need 'em */
  8. #include <intuition/intuition.h>
  9.  
  10. #include "wIcon.h"
  11.  
  12. /*
  13.  *  The revision levels needed to the libraries
  14.  */
  15.  
  16. #define INTUITION_REV   0
  17. #define GRAPHICS_REV    0
  18. extern struct IntuitionBase *IntuitionBase;
  19. extern struct GfxBase *GfxBase;
  20.  
  21.  
  22. /*
  23.  *  Return codes for error and OK
  24.  */
  25.  
  26. #define ERROR_EXIT      10
  27. #define OK_EXIT         0
  28.  
  29.  
  30. /*
  31.  *  Some routines we need
  32.  */
  33.  
  34. extern struct Window *OpenWindow();
  35. extern struct IntuiMessage *GetMsg();
  36. extern APTR OpenLibrary();
  37.  
  38.  
  39. /*
  40.  *  The definition for our window
  41.  */
  42.  
  43. static struct NewWindow NewWindow =
  44. {
  45.    160,50, 320,100, 0,1, CLOSEWINDOW| REFRESHWINDOW,
  46.    WINDOWCLOSE| WINDOWDRAG| WINDOWDEPTH| WINDOWSIZING| SIMPLE_REFRESH,
  47.    NULL, NULL, "wExample1", NULL, NULL, 20,10, -1,-1, WBENCHSCREEN
  48. };
  49. static struct Window *myWindow;
  50.  
  51.  
  52. /*
  53.  *  This is the image to be used by the icon for our window.
  54.  *  The image data should be in Chip ram, but we'll just compile the
  55.  *  whole program into chip, since this is just an example.
  56.  */
  57.  
  58. #define ICONWIDTH        41
  59. #define ICONHEIGHT       20
  60. #define ICONDEPTH        3
  61. #define WORDSPERROW      3
  62.  
  63. static USHORT IconData[ICONDEPTH][ICONHEIGHT*WORDSPERROW] =
  64. {
  65.   {
  66.     0x1FFF, 0xFFFF, 0x8000,
  67.     0x1C70, 0x003F, 0xC000,
  68.     0x0FFF, 0xFFFF, 0xC000,
  69.     0x0700, 0x0001, 0xC000,
  70.     0x0300, 0x0001, 0xC000,
  71.     0x010F, 0x0781, 0xC000,
  72.     0x0093, 0x0983, 0xC100,
  73.     0x0080, 0x0003, 0x8080,
  74.     0x0080, 0x0003, 0x8080,
  75.     0x0083, 0x0003, 0x1080,
  76.     0x1F80, 0xFE04, 0x0080,
  77.     0x0680, 0x0004, 0x0180,
  78.     0x0680, 0x007F, 0xFF00,
  79.     0x7D00, 0x00FE, 0x0000,
  80.     0x03FF, 0xFFFC, 0x0000,
  81.     0x0187, 0xF0F8, 0x0000,
  82.     0x0004, 0x0080, 0x0000,
  83.     0x0010, 0x0100, 0x0000,
  84.     0x0006, 0x0038, 0x0000,
  85.     0x01FE, 0x0FF8, 0x0000,
  86.   },
  87.   {
  88.     0x0000, 0x0001, 0x8000,
  89.     0x0000, 0x000C, 0xC000,
  90.     0x0000, 0x0000, 0xC000,
  91.     0x0000, 0x0000, 0xC000,
  92.     0x0000, 0x0000, 0xC000,
  93.     0x000F, 0x0780, 0xC000,
  94.     0x0010, 0x0801, 0xDF00,
  95.     0x0000, 0x0001, 0xBF80,
  96.     0x7F00, 0x0001, 0x8780,
  97.     0xFF00, 0x0001, 0xF780,
  98.     0xFF00, 0x0003, 0xFF80,
  99.     0xFE00, 0x0003, 0xFF80,
  100.     0xFE00, 0x0007, 0xFF00,
  101.     0x7C00, 0x000E, 0x0000,
  102.     0x0000, 0x001C, 0x0000,
  103.     0x01FF, 0xFFF8, 0x0000,
  104.     0x00FC, 0x0F80, 0x0000,
  105.     0x01F0, 0x1F00, 0x0000,
  106.     0x03FE, 0x3FF8, 0x0000,
  107.     0x01FE, 0x0FF8, 0x0000,
  108.   }
  109. };
  110.  
  111. static struct Image IconImage =
  112. {
  113.    0,0,
  114.    ICONWIDTH,ICONHEIGHT, ICONDEPTH,
  115.    &IconData[0][0], 0x03,0, NULL
  116. };
  117.  
  118. /*
  119.  *  This is the Icon Mask data.  It should be in CHIP RAM, too.
  120.  */
  121.  
  122. static UWORD IconMask[ICONHEIGHT*WORDSPERROW] =
  123. {
  124.   0x1FFF, 0xFFFF, 0x8000,
  125.   0x1FFF, 0xFFFF, 0xC000,
  126.   0x0FFF, 0xFFFF, 0xC000,
  127.   0x07FF, 0xFFFF, 0xC000,
  128.   0x03FF, 0xFFFF, 0xC000,
  129.   0x01FF, 0xFFFF, 0xC000,
  130.   0x00FF, 0xFFFF, 0xDF00,
  131.   0x00FF, 0xFFFF, 0xBF80,
  132.   0x7FFF, 0xFFFF, 0x8780,
  133.   0xFFFF, 0xFFFF, 0xF780,
  134.   0xFFFF, 0xFFFF, 0xFF80,
  135.   0xFEFF, 0xFFFF, 0xFF80,
  136.   0xFEFF, 0xFFFF, 0xFF00,
  137.   0x7DFF, 0xFFFE, 0x0000,
  138.   0x03FF, 0xFFFC, 0x0000,
  139.   0x01FF, 0xFFF8, 0x0000,
  140.   0x00FC, 0x0F80, 0x0000,
  141.   0x01F0, 0x1F00, 0x0000,
  142.   0x03FE, 0x3FF8, 0x0000,
  143.   0x01FE, 0x0FF8, 0x0000,
  144. };
  145.  
  146.  
  147. /*
  148.  *  This is the icon definition itself.  It is pretty simple; no special
  149.  *  flags or reports required by this one.
  150.  */
  151.  
  152. static WICON myIcon =
  153. {
  154.    "Super Icon!",       /* The name for the icon (default is the window name) */
  155.    &IconImage,          /* The icon's Image */
  156.    NULL,                /* No Select image (use inverted image) */
  157.    &IconMask[0],        /* The icon's Mask */
  158.    0,0,                 /* Let wIconify place it where it wants */
  159.    0,0,                 /* No special flags or reports */
  160.    NULL                 /* No IconPort needed since we're not getting reports */
  161. };
  162.  
  163.  
  164.  
  165. /*
  166.  *  DoExit()
  167.  *
  168.  *  Print an error message, if necessary, and then clean up any allocated
  169.  *  memory, close the windows and libraries, etc.  Finally, exit.
  170.  */
  171.  
  172. static void DoExit(s,x1,x2,x3)
  173. char *s, *x1,*x2,*x3;
  174. {
  175.    int status = OK_EXIT;
  176.  
  177.    if (s)
  178.    {
  179.       printf(s,x1,x2,x3);
  180.       printf("\n");
  181.       status = ERROR_EXIT;
  182.    }
  183.    if (myWindow)        CloseWindow(myWindow);
  184.    if (IntuitionBase)   CloseLibrary(IntuitionBase);
  185.    if (GfxBase)         CloseLibrary(GfxBase);
  186.    exit(status);
  187. }
  188.  
  189.  
  190. /*
  191.  *  CheckLibOpen()
  192.  *
  193.  *  Check to see if the specified library can be openned, and exit with
  194.  *  an error if not.
  195.  */
  196.  
  197. static void CheckLibOpen(lib,name,rev)
  198. APTR *lib;
  199. char *name;
  200. int rev;
  201. {
  202.    extern APTR OpenLibrary();
  203.    
  204.    if ((*lib = OpenLibrary(name,(ULONG)rev)) == NULL)
  205.       DoExit("Can't open '%s'",name);
  206. }
  207.  
  208.  
  209. /*
  210.  *  OpenMyWindow()
  211.  *
  212.  *  Attempt to open the window (error if unsuccessful).
  213.  *  Add the icon to the window once it's open, then iconify the window.
  214.  */
  215.  
  216. static void OpenMyWindow()
  217. {
  218.    myWindow = OpenWindow(&NewWindow);
  219.    if (myWindow == NULL) DoExit("Can't open my window");
  220.    wSetIcon(myWindow,&myIcon);
  221.    wIconify(myWindow);
  222. }
  223.  
  224.  
  225. /*
  226.  *  DisplayMessage()
  227.  *
  228.  *  Set the pen color, and write out the message telling the user
  229.  *  what he should do with this window.  Also show the icon for fun!
  230.  */
  231.  
  232. static void DisplayMessage()
  233. {
  234.    struct RastPort *rp = myWindow->RPort;
  235.  
  236.    SetAPen(rp,3);
  237.    Move(rp,110,35);
  238.    Text(rp,"Iconify Me!",11);
  239.    Move(rp,42,51);
  240.    Text(rp,"(Close me when you're done.)",28);
  241.    DrawImage(rp,&IconImage,
  242.       (myWindow->Width-ICONWIDTH)/2-4,myWindow->Height-ICONHEIGHT-10);
  243. }
  244.  
  245.  
  246. /*
  247.  *  WaitForAction()
  248.  *
  249.  *  While there is still more to do
  250.  *    Wait for a message to arrive at the window's UserPort
  251.  *    While there are more messages in the port
  252.  *      If the message class is a CLOSEWINDOW message, we're done
  253.  *      If its a REFRESH message, draw the text again.
  254.  *      Reply to the message
  255.  */
  256.  
  257. static void WaitForAction()
  258. {
  259.    struct IntuiMessage *theMessage;
  260.    short NotDone = TRUE;
  261.    struct MsgPort *thePort = myWindow->UserPort;
  262.    long SignalMask = (1 << thePort->mp_SigBit);
  263.  
  264.    while (NotDone)
  265.    {
  266.       Wait(SignalMask);
  267.       while (theMessage = GetMsg(thePort))
  268.       {
  269.          switch(theMessage->Class)
  270.          {
  271.             case CLOSEWINDOW:
  272.                NotDone = FALSE;
  273.                break;
  274.  
  275.             case REFRESHWINDOW:
  276.                BeginRefresh(myWindow);
  277.                DisplayMessage();
  278.                EndRefresh(myWindow);
  279.                break;
  280.          }
  281.          ReplyMsg(theMessage);
  282.       }
  283.    }
  284. }
  285.  
  286.  
  287. /*
  288.  *  main()
  289.  *
  290.  *  If wIconify is running
  291.  *    Open the libraries needed by the program
  292.  *    Open the window and add its icon
  293.  *    Display the message in the window
  294.  *    Wait for something to happen
  295.  *  Otherwise
  296.  *    Print an error message
  297.  *  Clean up afterwords
  298.  */
  299.  
  300. void main()
  301. {
  302.    if (wIconifyActive())
  303.    {
  304.       CheckLibOpen(&IntuitionBase,"intuition.library",INTUITION_REV);
  305.       CheckLibOpen(&GfxBase,"graphics.library",GRAPHICS_REV);
  306.       OpenMyWindow();
  307.       DisplayMessage();
  308.       printf("wExample1 window is open and iconified\n");
  309.       WaitForAction();
  310.    } else printf("wIconify not running or version mismatch\n");
  311.    DoExit(NULL);
  312. }
  313.